package app.netant.book.eshuu;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class EshuuDetail extends BaseDetail {

	public EshuuDetail(String netSite) {
		super(netSite);
		this.preHref = "http://www.eshuu.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select("#txtbook #txtleft .xiazai h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int idx = title.indexOf("作者：");
			if(idx>0){
				title = title.substring(0,idx).trim();			
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
		
		Elements parts1 = this.select("#txtbook #txtleft .xiazai h1 #author");
		if(parts1.size()>0){
			String writer = parts1.get(0).text();
			int idx = writer.indexOf("：");
			if(idx>0){
				writer = writer.substring(idx+1).trim();
				System.out.println("【Writer】:"+writer);
				this.entity.setNovelWriter(writer);
			}
		}
	}

	@Override
	public void analyseTags() {
		
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select("#txtbook #txtleft .xiazai .item .wrap .content p");
		if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select("#txtbook #txtleft .xiazai #txtdown.item ul li.bd a");
		if(parts.size()>0){
			String refer = parts.get(0).attr("href");
			if(refer.startsWith("/")){
				refer = this.preHref + refer;
			}
			System.out.println("【Refer】:"+refer);
			this.entity.setRefer(refer);
			
			EshuuDetail detail = new EshuuDetail(refer);
			String href = detail.stepHref();
			System.out.println("【Href】:"+href);
			this.entity.setDownPath(href);
		}
	}
	
	public String stepHref(){
		Elements parts = this.select(".wrap .con div h3 span span a");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			String href = parts.get(i).attr("href");
			if(text.indexOf("下载地址1")>=0){
				if(href.startsWith("..")){
					href = "http://www.eshuu.com/e/DownSys" + href.substring(2);
				}
				return href;
			}
		}
		return null;
	}
	
	@Override
	public void analyseType() {
		
	}
	
	public static void main(String[] args) {
		EshuuDetail detail = new EshuuDetail("http://www.eshuu.com/book/xiaoshuo/xuanhuan/7174.html");
		detail.test();
	}



}
